package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    @RequestMapping("")
    public String index(){
        return "department/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("department:data")
    @PermissionName("部门数据")
    public PageResult list(QueryObject qo){
        return service.query(qo);
    }

    @RequestMapping("queryDepts")
    @ResponseBody
    public List<Department> queryDepts(){
        return service.list();
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("department:save")
    @PermissionName("部门保存")
    public JsonResult save(Department department){
        JsonResult result = new JsonResult();
        try {
            service.save(department);
            System.out.println("22222222222222222222222");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("department:update")
    @PermissionName("部门更新")
    public JsonResult update(Department department){
        JsonResult result = new JsonResult();
        try {
            service.update(department);
            System.out.println("3333333333333333333333333");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("编辑失败");
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("department:changeState")
    @PermissionName("部门状态")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            service.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }
}
